package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;

import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/product")
public class SudnayProductController extends BaseController {

    @Autowired
    private SundayProductService productService;

    /**
     * 质押物  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                           Model model,
                         @RequestParam(value="type",required=true)Integer type){
        //商城商品
        if(type==1){
            return "/sunday/product/index/mall";
        //积分商品
        }else if(type==2){
            return "/sunday/product/index/score";
        }
       return "error";
    }

    /**
     * 分页查询质押物
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                        HttpServletResponse response,
                              @RequestParam(value="ids",required=false)List<Long> ids
    ) {
        try {
            PageResult<SundayProduct> result=productService.selectProduct(request,ids);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 商品详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="type",required=false)Integer type,
            Model model){

        SundayProduct product = productService.findOne(id);
        model.addAttribute("product", product);
        //商城商品
        if(type==1){
            return "/sunday/product/input/mall";
        }else if(type==2){
            //积分商品
            return "/sunday/product/input/score";
        }
        return "";
    }

    /**
     * 新增或修改商品
     * @param request
     * @param response
     * @param product
     * @param imageFile
     * @param rollImageFiles
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayProduct product,
                      @RequestParam(value="imageFile",required=false) MultipartFile imageFile,
                      @RequestParam(value="rollImageFiles",required=false) MultipartFile[] rollImageFiles
    ) {
        try {
            productService.save(product, imageFile, rollImageFiles);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/product/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    /**
     * 设置商品是否推荐。type:1首页推荐
     * @param request
     * @param response
     */
    @RequestMapping("/setRecommend")
    public  void setRecommend(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value="id",required=false) Long id,
                      @RequestParam(value="status",required=false) Integer status,
                      @RequestParam(value="type",required=false) Integer type

    ) {
        try {
            productService.setRecommend(id, status,type);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/product/setRecomment------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    /**
     * 上下架
     * @param request
     * @param response
     * @param id
     * @param status
     */
    @RequestMapping("/change")
    public  void change(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="id",required=false) Long id,
                              @RequestParam(value="status",required=false) Integer status

    ) {
        try {
            productService.change(id, status);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/product/setRecomment------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
   /**
     * 逐个删除某个质押物
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            productService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/product/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 产品选择器
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/product/choose/choose_erro";
        }else if(isSingle==1){
            return "/sunday/product/choose/choose_single";
        }else if(isSingle==0){
            return "/sunday/product/choose/choose_muilt";
        }

        return "/finance/product/choose/choose_erro";
    }

}
